<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <style>
    #test {
      transition-duration: 0.3s;
      transition-property: left;
      transition-timing-function: ease-in-out;
    }

    #btn {
      position: absolute;
      top: 40px;
      left: 0;
    }
  </style>
</head>

<body style="position: relative;">

  <div id="test" style="width: 100px;height:25px; background-color: burlywood;position: absolute;left: 0;"></div>
  <button id="btn">go</button>

</body>

</html>


<script>
  var arr = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];




  var index = 0;
  var loop = function () {
    index = index + 1;
    if (index < 10) {
      aysncFn(index, 'l');
      setTimeout(loop, 0);
    }
  }

  var rIndex = 0;
  var loopRight = function () {
    rIndex = rIndex + 1;
    if (rIndex < 10) {
      aysncFn(rIndex, 'r');
      setTimeout(loopRight, 0);

    }
  }

  $('#btn').click(function () {

    arr.forEach((i) => {
      aysncFn(i, 'l');
    })
    // loop();
    // var list = [1, 2];
    // for (let cIndex = 0; cIndex < list.length; cIndex++) {
    //   const item = list[cIndex];
    //   console.log('item:' + item);
    //   if (item == 1) {
    //     loop();
    //   } else {
    //     loopRight();
    //   }
    // }

  });

  function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
      continue;
    }
  }

  function aysncFn(i, d) {
    console.log('start', i);
    // 動畫
    // $('#test').animate({
    //   'left': ((i + 1) * 100) + 'px'
    // });
    if (d === 'l') {
      document.getElementById('test').style.left = ((i + 1) * 100) + 'px';
    } else {
      document.getElementById('test').style.right = ((i + 1) * 100) + 'px';
    }
    conso

    document.getElementById('test').innerText = i;
    document.getElementById('test').setAttribute('data-order', i);


    console.log('data-order:' + document.getElementById('test').getAttribute('data-order'));
    // console.log($('#test').css('left'));

    //.css('left', ((i + 1) * 100) + 'px');

    sleep(1000);

    console.log('end');
  }
</script>